﻿using System;
using Token.Piker.BLL;
using System.Web.UI.WebControls;
using Token.Piker.Common;

namespace Token.Piker.Web.Admin.Slide
{
    public partial class List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        //绑定数据
        private void BindData()
        {
            ViewSlideAdminBLL bll = new ViewSlideAdminBLL();
            if (pagerList.CurrentPageIndex == 1)
            {
                gridView.DataSource = bll.GetList(pagerList.PageSize, string.Empty, "AddTime DESC");
            }
            else
            {
                gridView.DataSource = bll.GetListByPage(string.Empty, "AddTime DESC", pagerList.StartRecordIndex, pagerList.EndRecordIndex);
            }
            gridView.DataBind();
            pagerList.RecordCount = bll.GetRecordCount("");
        }

        protected void pagerList_PageChanged(object sender, EventArgs e)
        {
            if (!IsPostBack && !IsCallback)
            {
                BindData();
            }
        }

        protected void ChangeState(object sender, EventArgs e)
        {
            LinkButton lkbChangeState = sender as LinkButton;
            SlideBLL slideBLL = new SlideBLL();
            if (lkbChangeState.Text == "未发布")
            {
                Token.Piker.Model.Slide slide = slideBLL.GetModel(Convert.ToInt32(lkbChangeState.CommandArgument));
                slide.SlideState = 1;
                slideBLL.Update(slide);
                BindData();
            }
            else
            {
                Token.Piker.Model.Slide slide = slideBLL.GetModel(Convert.ToInt32(lkbChangeState.CommandArgument));
                slide.SlideState = 0;
                slideBLL.Update(slide);
                BindData();
            }
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                GridViewRow gvr = ((LinkButton)e.CommandSource).Parent.Parent as GridViewRow;
                int index = gvr.RowIndex;
                int id = Convert.ToInt32(gridView.DataKeys[index].Value);
                SlideBLL slideBLL = new SlideBLL();
                if (slideBLL.Delete(id))
                {
                    //删除目录下的图片
                    //string src = e.CommandArgument.ToString();
                    //File.Delete(Server.MapPath(src)); 
                    BindData();
                }
                else
                {
                    MessageBox.Show(this.Page, "删除失败！");
                    return;
                }
            }
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lkbChangeState = e.Row.FindControl("lkbChangeState") as LinkButton;
                if (lkbChangeState.Text == "0")
                {
                    lkbChangeState.Text = "未发布";
                }
                else
                {
                    lkbChangeState.Text = "已发布";
                }
            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}